package com.ruoyi.web.schedule;

import com.ruoyi.web.constant.WalletConstant;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.enums.RechargeRecordEnum;
import com.ruoyi.web.mapper.*;
import com.ruoyi.web.service.IAppUserService;
import com.ruoyi.web.utils.IdWorker;
import com.ruoyi.web.utils.MysqlUtil;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 质押定时分配
 */
@Component
@EnableScheduling
@AllArgsConstructor
public class EmpowerTask {

    private final UserEmpowerMapper empowerMapper;
    private final AppUserMapper appUserMapper;
    private final UserEmpowerConfigMapper empowerConfigMapper;
    private final UserCapitalMapper capitalMapper;
    private final IdWorker idWorker;
    private final RechargeRecordMapper rechargeRecordMapper;
    private final MysqlUtil mysqlUtil;
    private final IAppUserService appUserService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void task() {
        List<AppUser> userList = appUserMapper.selectAppUserList(new AppUser());
        for (AppUser user : userList) {
            try {
                List<UserEmpower> empowerList = empowerMapper.selectUserEmpowerList(new UserEmpower() {{
                    setUserId(user.getId());
                }});
                if (empowerList.size() == 0) {
                    continue;
                }
                UserEmpower empower = empowerList.get(0);
                UserEmpowerConfig config = empowerConfigMapper.selectUserConfig(empower.getNum());
                if (config == null) {
                    continue;
                }
                BigDecimal divide = (empower.getNum().subtract(config.getMinCondition())).divide(config.getMaxCondition().subtract(config.getMinCondition()),8, RoundingMode.DOWN);
                BigDecimal add = config.getMinReward().add(config.getMaxReward().subtract(config.getMinReward()).multiply(divide));
                //
                BigDecimal reward = empower.getNum().multiply(add).setScale(2, RoundingMode.DOWN);
                mysqlUtil.startLocalTx();
                // 充值记录
                RechargeRecord record = new RechargeRecord();
                record.setId(idWorker.nextId());
                record.setUserId(user.getId());
                record.setCurrency(WalletConstant.USDT);
                record.setType(RechargeRecordEnum.Type.EMPOWER.getValue());
                record.setState(RechargeRecordEnum.Status.SUCCESS.getValue());
                record.setQuantity(reward);
                rechargeRecordMapper.insertRechargeRecord(record);
                capitalMapper.updateUserBalance(reward,user.getId());
                mysqlUtil.commitLocalTx();
                // 分红
                appUserService.miningDistribution(record,RechargeRecordEnum.Type.MINING_DISTRIBUTION.getValue());
            } catch (Exception e) {
                mysqlUtil.rollbackLocalTx();
            }
        }
    }

}
